import { IGlobalState } from "@/store";
import { SET_NEWS_LIST } from "@/store/home/actionTypes";
import { IHomeState, INewsInfo, IPostData, NAV_TYPES } from "@/typings";
import { computed, ComputedRef } from "@vue/runtime-core";
import { Store } from "vuex";

// 返回值是一个[{}] 它是ComputedRef类型
export function useNewList(store: Store<IGlobalState>): ComputedRef<INewsInfo[]> {
    const state: IHomeState = store.state.home;
    // 取出请求参数
    const type: NAV_TYPES = computed(() => state.currentType).value;
    const pageNum: number = computed(() => state.newsList.pageNum).value;
    const count: number = computed(() => state.newsList.count).value;
    // 最终的数据列表
    const newsList: ComputedRef<INewsInfo[]> = computed(() => state.newsList.news);
    
    store.dispatch(`home/${SET_NEWS_LIST}`, <IPostData>{
        type, pageNum, count
    });

    return newsList;
}